<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.util.logging.ErrorManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
	String useridMessage = null; //使用者帳號訊息，預先為空值 - 與下列if userid對應
	String nicknameMessage = null;
	String pw1Message = null; //使用者密碼訊息，預先為空值 - 與下列if pw1對應
	String emailMessage = null; //使用者信箱訊息，預先為空值- 與下列if email對應

	String userid = null;//宣告帳號值
	String nickname = null;
	String pw1 = null; //宣告密碼值 
	String email = null; //宣告信箱值

	if (request.getParameter("userid") != null) { //要求輸入參數名稱，即userid
		userid = request.getParameter("userid"); //輸入使用者帳號
		nickname = request.getParameter("nickname"); //輸入使用者帳號
		pw1 = request.getParameter("pw1"); //輸入使用者密碼
		email = request.getParameter("email"); //輸入使用者信箱
		boolean valid = true; //布林分析上述輸入是否錯誤

		if (userid.length() < 4 || userid.length() > 20) { //如果帳號小於4位或大於20位
			valid = false; //前一行經比較後輸入失敗
			useridMessage = "使用者帳號不符規定"; //顯示錯誤訊息-與String useridMessage對應
		}
		if (nickname.length() < 4 || nickname.length() > 20) { //如果帳號小於4位或大於20位
			valid = false; //前一行經比較後輸入失敗
			nicknameMessage = "使用者帳號不符規定"; //顯示錯誤訊息-與String useridMessage對應
		}
		if (!pw1.equals(pw1)) { //密碼
			valid = false;
			pw1Message = "密碼不符規定"; //顯示錯誤訊息-與String pw1Message對應
		}
		if (email.indexOf("@") == -1) { //信箱裡沒有@
			valid = false;
			emailMessage = "輸入不符規定"; //顯示錯誤訊息-與String emailMessage對應
		}
		System.out.println(valid); //先輸出測試看輸入值的true or flase

		if (valid) {
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			// Class.forName("com.mysql.jdbc.Driver"); // 舊的寫法
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
			// 資料庫連結
			Statement stmt = conn.createStatement();
			String sql = "INSERT INTO users(id , nickname , password , email) values('"
					+ userid
					+ "' ,'"
					+ nickname
					+ "' , '"
					+ pw1
					+ "' , '" + email + "')";
			System.out.println(sql);
			int rowCount = stmt.executeUpdate(sql);
			System.out.println(rowCount);
		}
	}
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show info</title>
</head>
<body>
 	<form action="act.jsp">
		USERNAME:<input type="text" id="userid" name="userid"
			value="<%=userid == null ? "" : userid%>" />
		<%=useridMessage == null ? "" : useridMessage%>
		<br> NICKNAME:<input type="text" id="nickname" name="nickname"
			value="<%=nickname == null ? "" : nickname%>" />
		<%=nicknameMessage == null ? "" : nicknameMessage%>
		<br> PASSWORD:<input type="text" id="pw1" name="pw1"
			value="<%=pw1 == null ? "" : pw1%>" />
		<%=pw1Message == null ? "" : pw1Message%>
		<br> EMAIL:<input type="text" id="email" name="email"
			value="<%=email == null ? "" : email%>" />
		<%=emailMessage == null ? "" : emailMessage%>
		<br> <input type="submit" value="註冊" />
	</form> 
</body>
</html>